/*******************************************************************************
 *   Filename:       bsp_opt300x.h
 *   Revised:        All copyrights reserved to Roger-WY.
 *   Revision:       v1.0
 *   Writer:	     Roger-WY.
 *
 *   Description:    opt300X光照度传感器驱动模块 头文件
 *******************************************************************************/
#ifndef __BSP_OPT300X_H__
#define __BSP_OPT300X_H__

#include "stm32g0xx_hal.h"
#include "bsp_timer.h"


#define  OPT3001_BV(n)      (1 << (n))


/* I2C SLAVE ADDRESS
|--------------------|----------|
| DEVICE I2C ADDRESS | ADDR PIN |
|--------------------|----------|
|      1000100       |   GND    |
|--------------------|----------|
|      1000101       |   VDD    |
|--------------------|----------|
|      1000110       |   SDA    |
|--------------------|----------|
|      1000111       |   SCL    |
|--------------------|----------|
*/
#define OPT3001_ADDRESS_GND		(0x88)   //芯片ADDR脚接GND
#define OPT3001_ADDRESS_VDD		(0x8A)   //芯片ADDR脚接VDD
#define OPT3001_ADDRESS_SDA		(0x8C)   //芯片ADDR脚接SDA
#define OPT3001_ADDRESS_ACL		(0x8E)   //芯片ADDR脚接SCL

//
//	Registers
//
#define OPT3001_RESULT		    0x00        //转换后的结果（光强度）寄存器
#define OPT3001_CONFIGURATION	0x01        //配置寄存器
#define OPT3001_LOW_LIMIT	    0x02        //下限寄存器
#define OPT3001_HIGH_LIMIT	    0x03        //上限寄存器
#define OPT3001_MANUFACTURER_ID	0x7e        //厂商ID寄存器
#define OPT3001_DEVICE_ID	    0x7f        //设备ID寄存器

#define OPT3001_CONFIGURATION_RN_MASK	    (0x0F << 12)
#define OPT3001_CONFIGURATION_RN_AUTO	    (0x0C << 12)

#define OPT3001_CONFIGURATION_CT	        OPT3001_BV(11)

#define OPT3001_CONFIGURATION_M_MASK	    (3 << 9)
#define OPT3001_CONFIGURATION_M_SHUTDOWN    (0 << 9)
#define OPT3001_CONFIGURATION_M_SINGLE	    (1 << 9)
#define OPT3001_CONFIGURATION_M_CONTINUOUS  (2 << 9) /* also 3 << 9 */

#define OPT3001_CONFIGURATION_OVF	    OPT3001_BV(8)
#define OPT3001_CONFIGURATION_CRF	    OPT3001_BV(7)
#define OPT3001_CONFIGURATION_FH	    OPT3001_BV(6)
#define OPT3001_CONFIGURATION_FL	    OPT3001_BV(5)
#define OPT3001_CONFIGURATION_L		    OPT3001_BV(4)
#define OPT3001_CONFIGURATION_POL	    OPT3001_BV(3)
#define OPT3001_CONFIGURATION_ME	    OPT3001_BV(2)

#define OPT3001_CONFIGURATION_FC_MASK	(3 << 0)

/* The end-of-conversion enable is located in the low-limit register */
#define OPT3001_LOW_LIMIT_EOC_ENABLE	0xC000

#define OPT3001_REG_EXPONENT(n)		((n) >> 12)
#define OPT3001_REG_MANTISSA(n)		((n) & 0xFFF)

#define OPT3001_INT_TIME_LONG		800000
#define OPT3001_INT_TIME_SHORT		100000

/*
 * Time to wait for conversion result to be ready. The device datasheet
 * sect. 6.5 states results are ready after total integration time plus 3ms.
 * This results in worst-case max values of 113ms or 883ms, respectively.
 * Add some slack to be on the safe side.
 */
#define OPT3001_RESULT_READY_SHORT	150
#define OPT3001_RESULT_READY_LONG	1000


#define OPT3001_MANUFACTURER_ID_VAL     0x5449  //设备厂商ID是0x5449,也就是ACSII码的"TI"
#define OPT3001_DEVICE_ID_VAL           0x3001  //设备ID

struct opt3001 {
	uint8_t			ok_to_ignore_lock;
	uint8_t			result_ready;
	uint16_t		result;

	uint32_t		int_time;
	uint32_t		mode;

	uint16_t		high_thresh_mantissa;
	uint16_t		low_thresh_mantissa;

	uint8_t			high_thresh_exp;
	uint8_t			low_thresh_exp;

	uint8_t			use_irq;
};

struct opt3001_scale {
	int32_t	val;
	int32_t	val2;
};

static const struct opt3001_scale opt3001_scales[] = {
	{
		.val = 40,
		.val2 = 950000,
	},
	{
		.val = 81,
		.val2 = 900000,
	},
	{
		.val = 163,
		.val2 = 800000,
	},
	{
		.val = 327,
		.val2 = 600000,
	},
	{
		.val = 655,
		.val2 = 200000,
	},
	{
		.val = 1310,
		.val2 = 400000,
	},
	{
		.val = 2620,
		.val2 = 800000,
	},
	{
		.val = 5241,
		.val2 = 600000,
	},
	{
		.val = 10483,
		.val2 = 200000,
	},
	{
		.val = 20966,
		.val2 = 400000,
	},
	{
		.val = 83865,
		.val2 = 600000,
	},
};

//============================================================================//
int8_t bsp_Opt300xInit(void);
int8_t bsp_Opt300xConfig(uint8_t addr);
int8_t bsp_ReadOpt300xLux(uint8_t addr,float *lux);

#endif
/***************************** (END OF FILE) **********************************/
